{%extends 'dbmp_base.html' %}

{% load staticfiles %}

<!-- 添加该页面自己需要的 css 模板 -->
{% block css %}
    <link href="{% static 'css/plugins/bootstrapvalidator/bootstrapValidator.min.css' %}" rel="stylesheet">
    <link href="{% static 'css/plugins/sweetalert/sweetalert.css' %}" rel="stylesheet">
{% endblock %}

{% block app_title %}业务组编辑{% endblock %}

<!-- 其他元素模块 -->
{% block top_element %}
<div class="row">
    <div class="col-md-5">
        <div class="row fontawesome-icon-list">
            <!-- 添加返回首页按钮 -->
            <div class="fa-hover col-md-4">
                <a href="{{ request.environ.HTTP_REFERER }}">
                    <i class="fa fa-reply"></i>
                    返回
                </a>
            </div>
        </div>
    </div>
    <div class="col-md-5">
        <div class="row fontawesome-icon-list">
            <!-- 添加返回MySQL实例列表按钮 -->
            <div class="fa-hover  col-md-4">
                <a href="{% url 'dbmp_mysql_business_index' %}">
                    <i class="fa fa-list"></i>
                    业务组列表
                </a>
            </div>
        </div>
    </div>
    <div class="col-md-2">
        <div class="row fontawesome-icon-list">
            <!-- 添加MySQL实例按钮 -->
            <div class="fa-hover col-md-10">
                <a href="{% url 'dbmp_mysql_business_add' %}">
                    <i class="fa fa-plus-square"></i>
                    添加业务组
                </a>
            </div>
        </div>
    </div>
</div>

{% endblock %}

<!-- 显示该页面主要展示的内容 -->
{% block content %}
<!-- OS 信息 -->
<section id="mysql_business">
    <h2 class="page-header">业务组信息</h2>

    <form method="post" action="{% url 'dbmp_mysql_business_edit' %}" class="form-horizontal m-t" id="default_form">
        {% csrf_token %}
        <input id="dmysql_business_id" name="mysql_business_id" type="hidden"
               value="{{ dbmp_mysql_business.mysql_business_id }}" />
        <div class="form-group">
            <label class="col-sm-3 control-label">
                <span class="text-danger">*</span>
                业务组名称：
            </label>
            <div class="col-sm-8">
                <input id="cname" name="name" 
                       type="text" class="form-control" maxlength="50"
                       value="{{ dbmp_mysql_business.name }}">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">
                <span class="text-danger">*</span>
                备注：
            </label>
            <div class="col-sm-8">
                <textarea id="cremark" name="remark" type="text" class="form-control">{{ dbmp_mysql_business.remark }}</textarea>
            </div>
        </div>
        <div class="form-group">
            <div class="col-sm-4 col-sm-offset-3">
                <a href="{{ request.environ.HTTP_REFERER }}" 
                   type="button" class="btn btn-default">
                    <i class="fa fa-times"></i>
                    取消
                </a>
                <button onclick="dbmp_mysql_business_delete({{ dbmp_mysql_business.mysql_business_id }})"
                   type="button" class="btn btn-danger">
                    <i class="fa fa-trash"></i>
                    删除
                </button>
                <button type="submit" class="btn btn-primary">
                    <i class="fa fa-save"></i>
                    保存
                </button>
            </div>
        </div>
    </form>
<section>
{% endblock %}

<!-- 添加该页面自己需要的 js 模板 -->
{% block js %}
    <script src="{% static 'js/content.min.js' %}"></script>
    <script src="{% static 'js/plugins/sweetalert/sweetalert.min.js' %}"></script>
    <script src="{% static 'js/plugins/bootstrapvalidator/bootstrapValidator.min.js' %}"></script>
    <script src="{% static 'js/plugins/bootstrapvalidator/language/zh_CN.js' %}"></script>
    <script src="{% static 'js/plugins/layer/layer.min.js' %}"></script>

    <script>
        // 使用 Django csrf 功能
        $.ajaxSetup({headers: {"X-CSRFToken": '{{ csrf_token }}'}});

        //删除 dbmp_mysql_business
        function dbmp_mysql_business_delete(mysql_business_id) {
            $(".confirm").unbind("click");
            swal({
                title: "您确定要删除这条信息吗?",
                text: "删除后将无法恢复，请谨慎操作！",
                type: "warning",
                showCancelButton: true,
                confirmButtonColor: "#DD6B55",
                confirmButtonText: "删除",
                closeOnConfirm: false
            }, function () {
                $.ajax({
                    url: "{% url 'dbmp_mysql_business_ajax_delete' %}",
                    type: "POST",
                    dataType: "json",
                    data: {
                        mysql_business_id: mysql_business_id
                    }
                }).done(function(data) { 
                    if(data == true) {
                        swal("删除成功!", "已成功删除数据！", "success");
                        $(".confirm").click(function(){
                            location.href="{% url 'dbmp_mysql_business_index' %}"
                        });
                    } else {
                        swal("对不起!", "删除数据失败了!", "error");
                    }
                }).error(function(data) { 
                    swal("对不起!", "删除操作失败了(调用删除方法失败)!", "error");
                });
            });
        }

        $(document).ready(function(){
            // 表单验证
            $('#default_form').bootstrapValidator({
                message: 'This value is not valid',
                feedbackIcons: {
                    valid: 'glyphicon glyphicon-ok',
                    invalid: 'glyphicon glyphicon-remove',
                    validating: 'glyphicon glyphicon-refresh'
                },
                fields: {
                    name: {
                        message: '用户名验证失败',
                        validators: {
                            notEmpty: {
                                message: '用户名不能为空'
                            },
                            stringLength: {
                                min: 1,
                                max: 50,
                                message: '用户名长度必须在1到50位之间'
                            }
                        }
                    },
                    remark: {
                        message: '备注验证失败',
                        validators: {
                            notEmpty: {
                                message: '备注不能为空, 主要是为了减少今后误操作'
                            },
                            stringLength: {
                                max: 200,
                                message: '备注长度必须在0到200位之间'
                            }
                        }
                    }
                }
            });
        });
        
    </script>
{% endblock %}
